package system

import (
	"bytes"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"ruoyi-go/app/admin/model/system"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/app/admin/service"
	"ruoyi-go/pkg/mysql"
	"ruoyi-go/utils"
	"ruoyi-go/utils/R"
)

// GetRoutersHandler /*
func GetRoutersHandler(context *gin.Context) {
	userId, _ := context.Get("userId")
	var user system.SysUser
	err1 := mysql.MysqlDb().Where("user_id = ?", userId).First(&user)

	if err1.Error != nil {
		panic("未找到用户")
		return
	}

	menu := service.SysMenuService.SelectMenuTreeByUserId(utils.GetInterfaceToInt(userId))
	var data = service.SysMenuService.BuildMenus(menu)
	R.SuccessData(context, data)
}

func ListMenu(context *gin.Context) {

	menuName := context.DefaultQuery("menuName", "")
	status := context.DefaultQuery("status", "")

	var param = tools.SearchTableDataParam{
		Other: system.SysMenu{
			MenuName: menuName,
			Status:   status,
		},
		Params: tools.Params{},
	}
	userId, _ := context.Get("userId")
	if service.SysUserService.IsAdminById(utils.GetInterfaceToInt(userId)) {
		var result = service.SysMenuService.SelectSysMenuList(param)
		R.SuccessData(context, result)
	} else {
		var result = service.SysMenuService.SelectSysMenuListByUserId(int(userId.(float64)), param)
		R.SuccessData(context, result)
	}
}

func GetMenuInfo(context *gin.Context) {
	userId, _ := context.Get("userId")
	println(userId)
	var menuId = context.Param("menuId")
	var date = service.SysMenuService.FindMenuInfoById(menuId)
	R.SuccessData(context, date)
}

func GetTreeSelect(context *gin.Context) {
	userId, _ := context.Get("userId")
	println(userId)
	data, _ := ioutil.ReadAll(context.Request.Body)
	// 这点很重要，把字节流重新放回 body 中
	context.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data))
	var menuParm system.SysMenu
	if err := context.ShouldBind(&menuParm); err != nil {
		panic("参数不能为空")
	}
	menu := service.SysMenuService.SelectMenuTree(utils.GetInterfaceToInt(userId), menuParm)
	var result = service.SysMenuService.BuildMenuTreeSelect(menu)
	R.SuccessData(context, result)
}

func TreeSelectByRole(context *gin.Context) {
	userId, _ := context.Get("userId")
	var roleId = context.Param("roleId")
	var menuPerms system.SysMenu
	menu := service.SysMenuService.SelectMenuTree(utils.GetInterfaceToInt(userId), menuPerms)
	var result = service.SysMenuService.BuildMenuTreeSelect(menu)
	roles := service.SysRoleService.FindRoleInfoById(roleId)
	var checkedKeys = service.SysMenuService.SelectMenuListByRoleId(roleId, roles.MenuCheckStrictly)
	res := make(map[string]any)
	res["menus"] = result
	res["checkedKeys"] = checkedKeys
	R.SuccessData(context, result)
}

func SaveMenu(context *gin.Context) {
	userId, _ := context.Get("userId")
	var menuParm system.SysMenu
	if err := context.ShouldBind(&menuParm); err != nil {
		panic("参数不能为空")
	}
	user := service.SysUserService.FindUserById(userId)
	menuParm.CreateBy = user.UserName
	service.SysMenuService.AddMenu(menuParm)
	R.Success(context)
}

func UploadMenu(context *gin.Context) {
	userId, _ := context.Get("userId")
	var menuParm system.SysMenu
	if err := context.ShouldBind(&menuParm); err != nil {
		panic("参数不能为空")
	}
	user := service.SysUserService.FindUserById(userId)
	menuParm.UpdateBy = user.UserName
	service.SysMenuService.UpdateMenu(menuParm)
	R.Success(context)
}

func DeleteMenu(context *gin.Context) {
	var menuId = context.Param("menuId")
	service.SysMenuService.DeleteMenu(menuId)
	R.Success(context)
}
